package com.albaba.designpattern.state_pattern.text_002;

/**
 * @Description: 交通灯类
 * @Author zx
 * @Date 2025/2/25 15:42
 * @Version 1.0
 * 红灯(禁行) 黄灯(警示) 绿灯(通行)
 */
public class TrafficLight {

    // 初始化状态
    private String state = "红色";

    // 切换为绿灯,通行状态
    public void switchToGreen() {
        if (state.contains("绿")) {
            System.out.println("绿灯已开启,通行");
        } else if (state.contains("红")) {
            System.out.println("红灯不能切换为绿灯");
        } else if (state.contains("黄")) {
            state = "绿";
            System.out.println("绿灯亮起....时长：60秒");
        }
    }

    // 切换为黄灯,警示状态
    public void switchToYellow() {
        if (state.contains("黄")) {
            System.out.println("当前为黄灯，无须切换");
        } else if (state.contains("红")|| state.contains("绿")) {
            state = "黄";
            System.out.println("黄灯亮起....时长：10秒");
        }
    }

    // 切换为红灯,禁行状态
    public void switchToRed() {
        if (state.contains("红")) {
            System.out.println("当前为红灯，无需切换");
        } else if (state.contains("绿")) {
            System.out.println("绿灯不能切换为红灯");
        } else if (state.contains("黄")) {
            state = "红";
            System.out.println("红灯亮起....时长：60秒");
        }
    }

}
